#include "plum_api.h"
#include "fcntl.h"
#include "sys/types.h"
#include <sys/stat.h>
#include <stdarg.h>


#define LOG_RUN_LEVEL  LOG_LEVEL_INFO
#define LOG_MODULE      "dev_main"
#include "thirdparty/log/log.h"

PLUM_PRIVATE plum_s8    KAL_TEST_THREAD_NAME[] = "TEST_thread"; 
PLUM_PRIVATE plum_s8    KAL_TEST_MQ_NAME[] = "TEST_mQ"; 
PLUM_PRIVATE plum_s8    KAL_TEST_MUTEX_NAME[] = "TEST_Mux"; 


typedef struct{
    plum_u32    id; 
    plum_void   *payload;
}plum_kal_mq_msg_t;

plum_void kal_test_thread(plum_void *arg)
{
    plum_s32 rc = PLUM_ECODE_OK; 

    plum_kal_mq_msg_t msg = {0};

    plum_kal_thread_delay(100);
    while(1){
        plum_kal_mutex_lock(KAL_TEST_MUTEX_NAME);
        LOG_I("this is sub thread");

        rc = plum_kal_mq_get(KAL_TEST_MQ_NAME,&msg,sizeof(msg),PLUM_KAL_MQ_WAIT_FOREVER);
        if(rc == PLUM_ECODE_OK){
            LOG_I("recv mq id:%d",msg.id);
            LOG_I("recv mq msg:%s",msg.payload);
            memset(&msg,0,sizeof(msg));
        }

        plum_kal_mutex_unlock(KAL_TEST_MUTEX_NAME);
        plum_kal_thread_delay(1);
    }
}

plum_s32    dev_cli_test(plum_u32 argc,plum_s8 **argv)
{
    LOG_I("this is plum test");

    return (0);
}
PLUM_CAL_CLI_EXPORT("test","cli test",dev_cli_test);

plum_s32    dev_cli_show(plum_u32 argc,plum_s8 **argv)
{
    plum_u8 loop;

    LOG_I("argc :   %d",argc);
    for(loop=0;loop<argc;loop++){
        LOG_I("argv[%d] : %s",loop,argv[loop]);
    }
    return (0);
}
PLUM_CAL_CLI_EXPORT("show","show arg",dev_cli_show);


#define     DEV_LOG_PATH    "dev.log"
static      int     dev_fd;

int dev_printf(const char *fmt,...)
{
    if(dev_fd){
        /* message */
        char	 buffer[1024] = {0};

        va_list ap;		

        va_start(ap, fmt);	
        (void)vsnprintf(buffer, sizeof(buffer), fmt, ap);
        va_end(ap);

        write(dev_fd,buffer,strlen(buffer));
    }
    else{
        printf("log file not init");
    }
}

int main(void)
{
    int rc = 0; 

    dev_fd = open(DEV_LOG_PATH,O_CREAT | O_RDWR | O_TRUNC,0666);
    if(!dev_fd){
        printf("open log file err");
    }
#if 0
    plum_u8 read[128] = {0};
    rc = plum_hal_flash_read(0,0,read,sizeof(read));
    if(rc <= 0){
        LOG_E("plum_hal_flash_read err,rc:%d");
    }
    LOG_IHEX("erase before read ",read,sizeof(read));

    rc = plum_hal_flash_erase(0,0,100);
    if(rc){
        LOG_E("plum_hal_flash_erase err,rc:%d",rc);
    }

    rc = plum_hal_flash_read(0,0,read,sizeof(read));
    if(rc <= 0){
        LOG_E("plum_hal_flash_read err,rc:%d");
    }
    LOG_IHEX("write before read ",read,sizeof(read));

    plum_u8 write[128] = "this is a flash test\r\n";
    rc = plum_hal_flash_write(0,0,write,sizeof(write));
    if(rc <= 0){
        LOG_E("plum_hal_flash_write err,rc:%d");
    }
        
    rc = plum_hal_flash_read(0,0,read,sizeof(read));
    if(rc <= 0){
        LOG_E("plum_hal_flash_read err,rc:%d");
    }
    LOG_IHEX("write after read ",read,sizeof(read));

   //plum_init();

    rc = plum_kal_mq_create(KAL_TEST_MQ_NAME,sizeof(plum_kal_mq_msg_t),1);
    if(rc){
        LOG_E("plum_kal_mq_create err,rc:%d",rc);
    }
    plum_kal_mq_show(plum_null);

    rc = plum_kal_mutex_create(KAL_TEST_MUTEX_NAME); 
    if(rc){
        LOG_E("plum_kal_mutex_create err,rc:%d");
    }  
    plum_kal_mutex_show(KAL_TEST_MUTEX_NAME); 

    rc = plum_kal_thread_create(KAL_TEST_THREAD_NAME,
                            kal_test_thread,
                            plum_null,
                            PLUM_KAL_THREAD_PRIORITY_30,
                            1024 * 8);
    if(rc){
        LOG_E("plum_kal_thread_create err,rc:%d",rc);
    }
    plum_kal_thread_show(plum_null);

    
    plum_u32 cnt = 4; 
    
    while(1){
        plum_kal_mutex_lock(KAL_TEST_MUTEX_NAME);

        LOG_I("#################This is in kal test####################");

        {
            plum_kal_mq_msg_t  msg = {0};
            msg.id = cnt++;
            msg.payload = "this is a mq test";
            plum_kal_mq_put(KAL_TEST_MQ_NAME,&msg,sizeof(msg),1000);

        }
        plum_kal_thread_delay(1000 * 3);

        plum_kal_mutex_unlock(KAL_TEST_MUTEX_NAME);
        plum_kal_thread_delay(1);
    }
#else
    log_print(dev_printf);

    plum_init();

    plum_cal_cli_init(getchar);
    while(1){
        plum_kal_thread_delay(100);
    }
#endif
    return (rc); 
}